import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    debugPaintSizeEnabled = true;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("images"),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              // 1. 加载网络图片
              Image.network(
                "http://pic2.sc.chinaz"
                ".com/Files/pic/pic9/202005/hpic2343_s.jpg",
                width: 100,
                height: 100,
              ),

              // 2. 加载assets
              Image.asset(
                'assets/images/img1.jpg',
                height: 200,
              ), //
              // 不指定大小的话默认使用图片大小

              // 3. 从memory中异步加载图片
              MemoryImageWidget(),

              // 4. 从文件加载图片
              FileImageWidget()
            ],
          ),
        ),
      ),
    );
  }
}

class FileImageWidget extends StatefulWidget {
  @override
  _FileImageWidgetState createState() => _FileImageWidgetState();
}

class _FileImageWidgetState extends State<FileImageWidget> {
  File _image;

  Future getImg() async {
//    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    Future<File> res = pickImageAndGetResult();
    res.then((value){
      setState(() {
        this._image = value;
      });
    });
    setState(() {
//      this._image = image;
    });
  }

  Future<File> pickImageAndGetResult() async {
    return await ImagePicker.pickImage(source: ImageSource.gallery);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Center(
          child: _image == null
              ? Text('未选择图片')
              : Image.file(
                  _image,
                  width: 200,
                  height: 200,
                ),
        ),
        FlatButton(
          onPressed: getImg,
          child: Text(
            '选择图片',
            style: TextStyle(color: Color(0xff0000ff)),
          ),
        )
      ],
    );
  }
}

class MemoryImageWidget extends StatefulWidget {
  @override
  _MemoryImageWidgetState createState() => _MemoryImageWidgetState();
}

class _MemoryImageWidgetState extends State<MemoryImageWidget> {
  Uint8List bytes;

  @override
  void initState() {
    super.initState();
    rootBundle.load('assets/images/img1.jpg').then((data) {
      if (mounted) {
        setState(() {
          bytes = data.buffer.asUint8List();
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final decoration = BoxDecoration(
      image: bytes == null ? null : DecorationImage(image: MemoryImage(bytes)),
    );
    return Container(
      width: 200,
      height: 200,
      decoration: decoration,
    );
  }
}
